package com.nanhai.buddhist.college.framework.mybatis.core.handler;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.nanhai.buddhist.college.framework.mybatis.core.util.IdGenerateUtil;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
public class CustomerIdGenerator implements IdentifierGenerator {
    @Override
    public boolean assignId(Object idValue) {
        boolean b = IdentifierGenerator.super.assignId(idValue);
        return b;
    }

    @Override
    public Number nextId(Object entity) {
        return 0;
    }

    @Override
    public String nextUUID(Object entity) {
        Class<?> aClass = entity.getClass();
        TableName annotation = aClass.getAnnotation(TableName.class);
        String tableName = "";
        String tableNewName = "";
        if (Objects.isNull(annotation)) {

        } else {
            tableName = annotation.value();
            List<String> split = StrUtil.split(tableName, '_', 3);
            tableNewName =  split.stream().map(s -> StrUtil.sub(s, 0, 1)).collect(Collectors.joining()).toUpperCase();
        }
        return IdGenerateUtil.getId(tableNewName);
    }
}
